<template>
  <el-dialog
    title="题目预览"
    :visible="showDialog"
    width="47%"
    @close="btnCancel"
  >
    <el-row type="flex" justify="space-between">
      <el-col>
        【题型】:{{ questionData.questionType | formatQuestionType }}
      </el-col>
      <el-col> 【编号】:{{ questionData.id }} </el-col>
      <el-col>
        【难度】: {{ questionData.difficulty | formatDifficulty }}
      </el-col>
      <el-col> 【标签】:{{ questionData.tags }} </el-col>
    </el-row>
    <el-row type="flex" justify="space-between" style="margin-top: 20px">
      <el-col> 【学科】: {{ questionData.subjectName }} </el-col>
      <el-col> 【目录】:{{ questionData.directoryName }} </el-col>
      <el-col> 【方向】:{{ questionData.direction }} </el-col>
      <el-col />
    </el-row>
    <hr style="margin-top: 20px">
    <div>【题干】:</div>
    <div v-html="questionData.question" />
    <!-- 单选 -->
    <div v-if="(questionData.questionType) * 1 === 1" style="padding-left: 30px">
      <p>单选题 选项:(以下的选中项为答案)</p>
      <div v-for="item in questionData.options" :key="item.id">
        <el-radio :value="radio" :label="item.title" style="margin-top: 10px" />
      </div>
    </div>
    <!-- 多选 -->
    <div v-if="(questionData.questionType) * 1 === 2" style="padding-left: 30px">
      <p>多选题 选项:(以下的选中项为答案)</p>
      <div
        v-for="item in questionData.options"
        :key="item.id"
        style="margin-top: 10px"
      >
        <el-checkbox-group v-model="checkBox">
          <el-checkbox :label="item.title" style="margin-top: 10px" />
        </el-checkbox-group>
      </div>
    </div>
    <hr>
    【参考答案】:
    <el-button
      type="danger"
      size="small"
      :disabled="!questionData.videoURL"
      @click="playVideo"
    >视频答案解析</el-button>
    <br>
    <video
      v-if="showVideo"
      style="width: 400px; height: 200px"
      :src="questionData.videoURL"
      controls
    />
    <hr>
    【答案解析】:
    <div style="display: inline-block" v-html="questionData.answer" />
    <hr>
    【题目备注】:
    <div style="display: inline-block" v-html="questionData.remarks" />
    <el-row slot="footer" type="flex" justify="end">
      <el-col>
        <el-button type="primary" @click="btnCancel">关闭</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 基础题库详情
import { detail } from '@/api/question/index'
// 组题类型 难度 枚举
import { questionType, difficulty } from '@/api/question/constants'
export default {
  filters: {
    // 组题类型
    formatQuestionType(val) {
      const obj = questionType.find((item) => item.value * 1 === val * 1)
      return obj ? obj.label : '未知'
    },
    // 难度
    formatDifficulty(val) {
      const obj = difficulty.find((item) => item.value * 1 === val * 1)
      return obj ? obj.label : '未知'
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      questionData: {}, // 题目信息
      radio: '', // 单选
      checkBox: [], // 多选
      showVideo: false // 视频显示与隐藏
    }
  },
  methods: {
    async getQuestionDetail(id) {
      // console.log(id)
      const { data: res } = await detail({ id })
      this.questionData = res
      // console.log(this.questionData)
      if (res.questionType === '3') return
      this.radio = res.options.find((item) => item.isRight === 1).title
      this.checkBox = res.options
        .filter((item) => item.isRight === 1)
        .map((item) => item.title)
    },
    // 关闭取消
    btnCancel() {
      this.questionData = {}
      this.checkBox = []
      this.radio = ''
      this.showVideo = false
      this.$emit('update:showDialog', false)
    },
    playVideo() {
      if (!this.questionData.videoURL) {
        return this.$message.warning('此题目没有视频解析')
      }
      this.showVideo = true
    }
  }
}
</script>

